package com.baoxin.chenxixc4coach;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.baoxin.chenxixc4coach.adapter.DateAdapter;
import com.baoxin.chenxixc4coach.adapter.ScheduleAdapter;
import com.baoxin.chenxixc4coach.bean.Schedule;
import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 日事清
 */
public class ScheduleActivity extends BaseActivity implements DateAdapter.DateSelectedLisener {
    private RecyclerView recyclerView;//日期
    private DateAdapter dateAdapter;

    private RecyclerView recyclerView2;//待办事项
    private ScheduleAdapter scheduleAdapter;
    private List<Schedule> scheduleList;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Msg.REQUEST_SUCCEED:
                        scheduleList = (List<Schedule>) msg.obj;
                        scheduleAdapter.setScheduleList(scheduleList);
                        scheduleAdapter.notifyDataSetChanged();
                        break;
                }
            }
        };
        recyclerView = findViewById(R.id.recyclerview_date);
        dateAdapter = new DateAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(dateAdapter);

        recyclerView2 = findViewById(R.id.recyclerview);
        scheduleAdapter = new ScheduleAdapter();
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
        layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView2.setLayoutManager(layoutManager2);
        recyclerView2.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView2.setAdapter(scheduleAdapter);

    }

    private String selectDate;// 2018/11/30

    @Override
    public void updateCourse(String riqi) {
        selectDate = riqi;
        initData(riqi);
    }

    private void initData(String riqi) {

        JSONObject body = new JSONObject();
        try {
            body.put("page", 1);
            body.put("limit", 100);
            body.put("rsqTime ", riqi);
            NetUtil.post(activity, Interfaces.URL_AGENDA_LIST, body.toString(), new MyCallback(Interfaces.URL_AGENDA_LIST, handler));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 创建日程
     *
     * @param view
     */
    public void onAdd(View view) {
        Intent intent = new Intent(this, AgendaEditActivity.class);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0) {
            initData(selectDate);
        }
    }
}
